package org.example.controller.post;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.example.annotation.HasRole;
import org.example.dao.commom.Result;
import org.example.dao.commom.ResultCode;
import org.example.dao.dto.post.PCommentDto;
import org.example.dao.entity.post.PComment;
import org.example.service.post.CommentService;
import org.example.skytool.commom.ParamUtil;
import org.simpleframework.xml.Path;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 22449
 */
@RestController
@RequestMapping("/post")
@CrossOrigin
public class CommentController {

    //TODO 功能

    /**
     * 点赞评论(用户)
     */
    @PostMapping("likeComment")
    @HasRole(role = "用户")
    public Result likeComment(@RequestBody PComment tComment) {
        Long id = tComment.getId();

        ParamUtil.LongParam(id);

        Long tComment1 = commentPost.likeComment(id);
        return new Result(ResultCode.SUCCESS, tComment1);
    }


    //TODO 新增

    /**
     * 添加评论(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("commentPost")
    public Result commentPost(@RequestBody PComment tComment) {
        Long postId = tComment.getPostId();
        String content = tComment.getContent();

        ParamUtil.LongParam(postId);
        ParamUtil.StringParam(content);

        PCommentDto tComment1 = commentPost.commentPost(postId, content);
        return new Result(ResultCode.SUCCESS, tComment1);
    }


    //TODO 查询

    /**
     * 获取对我的评论(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("getCommentByToMine/{limit}/{start}")
    public Result getCommentByToMine(
            @RequestBody PComment pComment,
            @PathVariable("limit") Long limit,
            @PathVariable("start") Long start) {

        Integer isread = pComment.getIsRead();

        List<PCommentDto> iPage = commentPost.getCommentByToMine(isread,limit,start);
        return new Result(ResultCode.SUCCESS, iPage);
    }

    /**
     * 获取评论(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("getCommentByPost/{limit}/{start}")
    public Result getCommentByPost(@RequestBody PComment tComment,
                                   @PathVariable("limit") Integer limit, @PathVariable("start") Integer start) {
        Long postId = tComment.getPostId();

        ParamUtil.LongParam(postId);

        IPage<PCommentDto> list = commentPost.getCommentByPost(postId, start, limit);
        return new Result(ResultCode.SUCCESS, list);
    }

    /**
     * 获取本人所有评论(用户)
     */
    @PostMapping("getCommentByMine/{limit}/{start}")
    public Result getCommentByMine(@PathVariable("limit") Integer limit,
                                   @PathVariable("start") Integer start) {
        IPage<PCommentDto> list = commentPost.getCommentByMine(limit, start);
        return new Result(ResultCode.SUCCESS, list);
    }


    //TODO 更新

    /**
     * 更新评论(用户)
     */
    @PostMapping("updateComment")
    public Result updateComment(@RequestBody PComment pComment) {
        Long id = pComment.getId();
        String content = pComment.getContent();

        ParamUtil.LongParam(id);
        ParamUtil.StringParam(content);

        PCommentDto pCommentDto = commentPost.updateComment(id, content);
        return new Result(ResultCode.SUCCESS, pCommentDto);
    }


    //TODO 删除

    /**
     * 删除评论(用户)
     */
    @PostMapping("deleteComment")
    public Result deleteComment(@RequestBody PComment pComment) {
        Long id = pComment.getId();

        ParamUtil.LongParam(id);

        commentPost.deleteComment(id);
        return new Result(ResultCode.SUCCESS, null);
    }


    @Resource
    private CommentService commentPost;


}
